﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Net.Nntp.Cache
{
    using System.Data;
    using System.Data.SqlServerCe;

    /// <summary>Provides helper methods to simplify the creation of <see cref="SqlCeCommand"/> objects.</summary>
    internal static class CommandHelper
    {
        private const string PrimaryKeyName = "PrimaryKey";

        /// <summary>Creates a new <see cref="SqlCeCommand"/> object.</summary>
        /// <param name="connection">The connection to use.</param>
        /// <param name="tableName">The name of the table on which the command will operate. It is assumed that the
        /// table has an index with the name "PrimaryKey".</param>
        internal static SqlCeCommand Create(SqlCeConnection connection, string tableName)
        {
            return Create(connection, tableName, PrimaryKeyName);
        }

        /// <summary>Creates a new <see cref="SqlCeCommand"/> object.</summary>
        /// <param name="connection">The connection to use.</param>
        /// <param name="tableName">The name of the table on which the command will operate.</param>
        /// <param name="indexName">The name of the index that will be used.</param>
        internal static SqlCeCommand Create(SqlCeConnection connection, string tableName, string indexName)
        {
            var command = new SqlCeCommand(tableName, connection);
            command.CommandType = CommandType.TableDirect;
            command.IndexName = indexName;
            return command;
        }

        /// <summary>Creates a new <see cref="SqlCeCommand"/> object.</summary>
        /// <param name="connection">The connection to use.</param>
        /// <param name="tableName">The name of the table on which the command will operate. It is assumed that the
        /// table has an index with the name "PrimaryKey".</param>
        /// <param name="rangeOptions">See the first parameter of <see cref="SqlCeCommand.SetRange"/>.</param>
        /// <param name="startData">See the second parameter of <see cref="SqlCeCommand.SetRange"/>.</param>
        /// <param name="endData">See the third parameter of <see cref="SqlCeCommand.SetRange"/>.</param>
        internal static SqlCeCommand Create(
            SqlCeConnection connection,
            string tableName,
            DbRangeOptions rangeOptions,
            object[] startData,
            object[] endData)
        {
            return Create(connection, tableName, PrimaryKeyName, rangeOptions, startData, endData);
        }

        /// <summary>Creates a new <see cref="SqlCeCommand"/> object.</summary>
        /// <param name="connection">The connection to use.</param>
        /// <param name="tableName">The name of the table on which the command will operate.</param>
        /// <param name="indexName">The name of the index that will be used.</param>
        /// <param name="rangeOptions">See the first parameter of <see cref="SqlCeCommand.SetRange"/>.</param>
        /// <param name="startData">See the second parameter of <see cref="SqlCeCommand.SetRange"/>.</param>
        /// <param name="endData">See the third parameter of <see cref="SqlCeCommand.SetRange"/>.</param>
        internal static SqlCeCommand Create(
            SqlCeConnection connection,
            string tableName,
            string indexName,
            DbRangeOptions rangeOptions,
            object[] startData,
            object[] endData)
        {
            var command = new SqlCeCommand(tableName, connection);
            command.CommandType = CommandType.TableDirect;
            command.IndexName = indexName;
            command.SetRange(rangeOptions, startData, endData);
            return command;
        }
    }
}
